
package bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import dao.CharacterDAO;

public class MailBEAN {
	private int			mailID;	// this also is the characterID and the
									// mailToID
	private int			mailFromID, mailToID;
	private String		title, content;
	private Calendar	dateTime;
	private boolean		isRead;

	public MailBEAN( int mailID, int mailFromID, int mailToID, String title,
			String content, Calendar dateTime, boolean isRead ) {
		super();
		this.mailID = mailID;
		this.mailFromID = mailFromID;
		this.mailToID = mailToID;
		this.title = title;
		this.content = content;
		this.dateTime = dateTime;
		this.isRead = isRead;
	}

	@Override
	public String toString() {
		return "MailBEAN [mailID=" + mailID + ", mailFromID=" + mailFromID
				+ ", mailToID=" + mailToID + ", title=" + title + ", content="
				+ content + ", isRead=" + isRead + "]";
	}

	public String getDateTimeConverted() {
		return getStringFromCalendar( dateTime );
	}

	public String getSender() {
		return CharacterDAO.getNameByID( mailFromID );
	}

	public int getMailID() {
		return mailID;
	}

	public void setMailID( int mailID ) {
		this.mailID = mailID;
	}

	public int getMailFromID() {
		return mailFromID;
	}

	public void setMailFromID( int mailFromID ) {
		this.mailFromID = mailFromID;
	}

	public int getMailToID() {
		return mailToID;
	}

	public void setMailToID( int mailToID ) {
		this.mailToID = mailToID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle( String title ) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent( String content ) {
		this.content = content;
	}

	public Calendar getDateTime() {
		return dateTime;
	}

	public void setDateTime( Calendar dateTime ) {
		this.dateTime = dateTime;
	}

	public boolean isRead() {
		return isRead;
	}

	public void setRead( boolean isRead ) {
		this.isRead = isRead;
	}

	public static String getStringFromCalendar( Calendar time ) {
		if( time == null )
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return sdf.format( time.getTime() );
	}

	/**
	 * 
	 * @param time
	 *            format = yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Calendar getCalendarFromString( String time ) {
		if( time == null ) { return new GregorianCalendar(); }
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		Date date = null;
		try {
			date = (Date) sdf.parse( time );
		} catch( ParseException e ) {
			e.printStackTrace();
			return new GregorianCalendar();
		}
		Calendar cal = new GregorianCalendar();
		cal.setTime( date );
		return cal;
	}
}
